<?php

require_once("appinclude.php");
require_once("facebook.php");
require_once("ICSReader.php");
// establish link with mysql database
$link = @mysql_connect($GLOBALS["db_host"], $GLOBALS["db_user"], $GLOBALS["db_pass"]);
@mysql_select_db($GLOBALS["db_name"]);

$facebook = new Facebook($GLOBALS["api_key"], $GLOBALS["api_secret"]);
$user = $facebook->require_login();

$user = isset($_POST['fb_sig_user']) ? $_POST['fb_sig_user'] : null;
$cal_sem = $_POST['cal_sem'];

$cal_strings = get_cal_strings(get_friends($user));
$friends = array_keys($cal_strings);

// create hashtable - friend_id -> calendar
$friend_cals = array();
foreach($friends as $friend)
{
    $url = 'http://scheduleman.org/schedules/'.$cal_strings[$friend].'.ics';
    $friend_cals[$friend] = new ICSReader;
    $friend_cals[$friend]->iCalReader($url);
}

// go through my classes ana each class
$my_calendar = new ICSReader;
$my_calendar->iCalReader('http://scheduleman.org/schedules/'.get_cal_string($user).'.ics');
$my_events = $my_calendar->getEvents();

foreach($my_events as $event)
{
    // loop through friends
    print '<br /><br /><b>'.$event['SUMMARY'].' | '.$event['DESCRIPTION'];
    foreach($friends as $friend)
    {
        $friend_events = $friend_cals[$friend]->getEvents();
        foreach($friend_events as $friend_event)
        {
            if($friend_event['SUMMARY'] == $event['SUMMARY'])
                print '<br /><fb:name uid="'.$friend.'" />';
        }
    }
}


?>